<?php

include_once dirname(__FILE__) . "/../log.php";
include_once dirname(__FILE__) . "/../conf.php";

class Log_test extends PHPUnit_Framework_TestCase
{
    public function test_init_with_conf_error()
    {
        $cur_dir = dirname(__FILE__);
        $cmd = "cp $cur_dir/../conf/conf.ini $cur_dir/../conf/conf.ini.old";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
        file_put_contents("$cur_dir/../conf/conf.ini", "this line is not ini file format");
        
        global $err_code;
        $ret = Log::init("Log_test");
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_conf_error);
        
        $cmd = "mv $cur_dir/../conf/conf.ini.old $cur_dir/../conf/conf.ini";
        passthru($cmd, $ret);
        $this->assertTrue($ret == 0);
    }
    
    public function test_init_with_perm_error()
    {
        $path = Conf::get("log.path"); 
        $ret = chmod($path, 0555);
        $this->assertTrue($ret);
        
        global $err_code;
        $ret = Log::init("Log_test");
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_perm_error);
        
        $ret = chmod($path, 0775);
        $this->assertTrue($ret);
    }
    
    public function test_write_log_failed()
    {
        $path = Conf::get("log.path"); 
        $ret = chmod($path, 0555);
        $this->assertTrue($ret);
        
        global $err_code;
        $ret = Log::init("Log_test");
        $this->assertTrue($ret == false);

        $ret = Log::write_log(Log::LOG_DEBUG, "this line will not be written.");
        $this->assertTrue($ret == false);
        $this->assertTrue($err_code == ec_perm_error);

        $ret = chmod($path, 0775);
        $this->assertTrue($ret);
    }
    
    
    public function test_debug_succ()
    {
        global $err_code;
        $ret = Log::init("Log_test");
        $this->assertTrue($ret);
        $ret = Log::debug("this line is debug info.");
        $this->assertTrue($ret);
        $this->assertTrue($err_code == ec_ok);
    }
    
    public function test_error_succ()
    {
        global $err_code;
        $ret = Log::init("Log_test");
        $this->assertTrue($ret);
        $ret = Log::error("this line is error info.");
        $this->assertTrue($ret);
        $this->assertTrue($err_code == ec_ok);
    }
    public function test_write_log_succ()
    {
        global $err_code;
        $ret = Log::init("Log_test");
        $this->assertTrue($ret);
        $ret = Log::write_log(Log::LOG_ERROR, "this line will be written.");
        $this->assertTrue($ret);
        $this->assertTrue($err_code == ec_ok);
    }
}

?>
